package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.web.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Employee record) {
         employeeMapper.insert(record);
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Employee record) {
        employeeMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Employee> que2(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectLimit(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public void insertByRoleId(Long id, Long ids) {
        employeeMapper.insertByRoleId(id,ids);
    }

    @Override
    public Employee selectOne(String username,String password) {
        if (StringUtils.isEmpty(username)){
            throw new BusinessException("用户名不能为空");
        }
        if (StringUtils.isEmpty(password)){
            throw new BusinessException("密码不能为空");
        }
        Employee employee = employeeMapper.selectOne(username);
        if (employee==null || !employee.getPassword().equals(password)){
            throw  new BusinessException("账号密码错误");
        }
        return employee;
    }

    @Override
    public void deleteByRoleId(Long id) {
        employeeMapper.deleteByRoleId(id);
    }


//    导出表
    @Override
    public void exprotXls(ServletOutputStream outputStream) throws IOException {
//        创建表
        Workbook wb = new HSSFWorkbook();
//        定义表的页
        Sheet sheet = wb.createSheet("员工列表");
//        定义行
        Row row = sheet.createRow(0);
//        定义定一行头部信息
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工年龄");
        row.createCell(3).setCellValue("员工邮箱");
//        把查到的员工的信息放进表中
        List<Employee> employees = employeeMapper.selectAll();
        for (int i=0;i<employees.size();i++){
            Employee employee = employees.get(i);
             row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getAge());
            row.createCell(3).setCellValue(employee.getEmail());
        }
//        将文件输出到浏览器
           wb.write(outputStream);
    }

//    导入
    @Override
    public void importXls(MultipartFile file) throws IOException {
        //把接收到的文件以excel的方式去读取并操作
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        //读取第一页
        Sheet sheet = wb.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        //第一行的模板头已经确定,所以从索引为1的行数开始读(忽略标题行)
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);

            String username = row.getCell(0).getStringCellValue();
            if (username==null || "".equals(username)){
                return;
            }
            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setName(row.getCell(1).getStringCellValue());
            Double numericCellValue = row.getCell(2).getNumericCellValue();
            employee.setAge(numericCellValue.intValue());
            employee.setEmail(row.getCell(3).getStringCellValue());
            employee.setAdmin(false);
            employee.setPassword("123456");
            Department department = new Department();
            department.setId(2L);
            employee.setDept(department);
            employeeMapper.insert(employee);
        }
    }

    @Override
    public Employee getEmployeeName(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
}
